import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Query,
  Post,
  Put,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { ILike } from 'typeorm';
import { IsAuth } from 'src/decorators/auth.decorator';
import { IResponseData, responseData } from 'src/utils/result';
import { DictDataService } from './dictData.service';
import {
  CreateDictDataDto,
  FindDictDataListDto,
  DictDataDto,
  UpdateDictDataDto,
} from './dictData.dto';
@ApiTags('字典模块')
@ApiBearerAuth()
@Controller('dictData')
export class DictDataController {
  constructor(private readonly dictDataService: DictDataService) {}
  @ApiOperation({ summary: '查询字典列表' })
  @ApiResponse({
    status: 200,
    description: '查询字典列表',
    type: DictDataDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  @IsAuth('system:dictData:list')
  async findList(
    @Query() dto: FindDictDataListDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictDataService.getLists({
      where: {
        ...dto,
        dict_label: dto.dict_label ? ILike('%' + dto.dict_label + '%') : null,
        dict_type: dto.dict_type ? ILike('%' + dto.dict_type + '%') : null,
      },
      order: {
        sort: 'ASC',
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询字典类型' })
  @ApiResponse({
    status: 200,
    description: '查询字典类型',
    type: DictDataDto,
  })
  @Get('findByType/:dict_type')
  async findByType(
    @Param('dict_type') dict_type: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    console.log(dict_type);
    const res = await this.dictDataService.getAll({
      where: {
        dict_type: dict_type,
      },
      order: {
        sort: 'ASC',
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询字典详情' })
  @ApiResponse({
    status: 200,
    description: '查询字典详情',
    type: DictDataDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(':id')
  @IsAuth('system:dictData:detail')
  async find(
    @Param('id') id: number,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictDataService.getOne({
      where: {
        id,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @Post()
  @IsAuth('system:dictData:add')
  @ApiOperation({ summary: '创建字典' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async create(
    @Body() dto: CreateDictDataDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictDataService.create(dto);
    return responseData({
      code: 100020,
      data: res,
      language: headers['language'],
    });
  }

  @Put()
  @IsAuth('system:dictData:update')
  @ApiOperation({ summary: '更新字典' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async update(
    @Body() dto: UpdateDictDataDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictDataService.update(dto);
    return responseData({
      code: 100030,
      data: res,
      language: headers['language'],
    });
  }

  @Delete()
  @IsAuth('system:dictData:delete')
  @ApiOperation({ summary: '删除字典' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async delete(
    @Query('ids') query: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictDataService.delete(query);
    return responseData({
      code: 100040,
      data: res,
      language: headers['language'],
    });
  }
}
